<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/zTree/v3/css/metroStyle/metroStyle.css" type="text/css">
</head>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-3">
				<input id="organizationId" name="organizationId" type="hidden" value="" /> <input id="parentId" name="parentId" type="hidden" value="" />
				<ul id="organizationTree" class="ztree" style="margin: 10px 0 0 15px;"></ul>
			</div>
			<div class="col-sm-9">
				<div class="col-sm-12">
					<div class="row">
						<table id="table">
							<thead style="background-color: #00C0EF">
								<tr style="background-color: #00C0EF">
									<th class="text-center col-sm-2" data-field="directoriesName">部门名称</th>
									<th class="text-center col-sm-3" data-field="name">姓名</th>
									<th class="text-center" data-field="tel">电话</th>
									<th class="text-center col-sm-2" data-field="id">操作</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
		</div>
	</section>
		
		<div id="modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">通讯录编辑</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="user_name" class="control-label">姓名:</label> 
						<input type="text" class="form-control" id="user_name" name="name">
					</div>
					<div class="form-group">
						<label for="user_tel" class="control-label">电话:</label> 
						<input type="text" class="form-control" id="user_tel" name="tel" onblur="checkTel();"
						onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)">
					</div>
					<div class="form-group">
						<label for="community_name" class="control-label">所属部门:</label> 
						<input id="organizationName" name="organizationName" type="text" class="form-control" value="" readonly="readonly"/> 
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" onclick="submitDirectories();">提交</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/plugins/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var zTree;
	var checkedNode = null;
	var url;
	var checkCodeFlag = false;

	var setting = {
		data : {
			key : {
				children : 'children',
				name : 'organizationName'
			}
		},
		view : {
			txtSelectedEnable : true
		},
		callback : {
			onClick : function(event, treeId, treeNode) {
				checkedNode = treeNode;
				nodeClick(treeNode);
			}
		}
	};

	$(function() {
		loadTree();
	});

	function loadTree() {
		//查询组织树,初始化ztree
		$.ajax({
			url : "${pageContext.request.contextPath}/selectOrganizationTree",
			type : "get",
			dataType : "json",
			async : true,
			success : function(data) {
				if (data.length > 0) {
					$.fn.zTree.init($("#organizationTree"), setting, data);
					zTree = $.fn.zTree.getZTreeObj("organizationTree");
					zTree.expandAll(true);
				} else {
					resetOrganization();
					url = '${pageContext.request.contextPath}/insertOrganization';
					layer.alert('请先添加一个根节点!');
					checkCodeFlag = true;
				}
			},
			error : function(error) {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
			}
		});
	}

	function nodeClick(treeNode) {
		if (treeNode !== null) {
			$('#organizationId').val(treeNode.organizationId);
			$('#organizationName').val(treeNode.organizationName);
			$('#organizationCode').val(treeNode.organizationCode);
			$('#organizationPrincipal').val(treeNode.organizationPrincipal === undefined ? '' : treeNode.organizationPrincipal);
			$('#remark').val(treeNode.remark === undefined ? '' : treeNode.remark);
			$('#editAccount').text(treeNode.editAccount);
			$('#editTime').text(treeNode.editTime === undefined ? '' : treeNode.editTime);
			var parentNode = treeNode.getParentNode();
			$('#parentName').text(parentNode === null ? '' : parentNode.organizationName);
			$("#table").bootstrapTable('refresh');
		}
	}
	
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-primary btn-xs glyphicon glyphicon-pencil' onclick='showModal(\"" + row.id + "\");'>编辑</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delDirectories(\"" + row.id + "\");'>删除</button>", ].join('');
	}
	var table_dataset = [{
		field : 'organizationName',
		align : 'center',
	}, {
		field : 'name',
		align : 'center',
	}, {
		field : 'tel',
		align : 'center',
	}, {
		align : 'center',
		width : 150,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/selectDirctories",
		idField : "id",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit,
				organizationId:$('#organizationId').val()
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
	});
	
	var dieId = "";
	function showModal(id) {
		dieId = id;
		if($('#organizationId').val() == ""){
			layer.msg('请选择部门!', {
				icon : 2,
				time : 800
			});
			return false;
		}
		if (id == "") {
			$("#user_name").val('');
			$("#user_tel").val('');
			$('#modal').modal({
				backdrop : 'static',
				keyboard : false
			});
		} else {
			$.ajax({
				url : "${pageContext.request.contextPath}/selectDirctoriesById",
				type : "get",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					var dir = data['directories'];
					$("#user_name").val(dir.name);
					$("#user_tel").val(dir.tel);
					$('#modal').modal({
						backdrop : 'static',
						keyboard : false
					});
				},
				error : function() {
					layer.msg('系统异常!', {
						icon : 2,
						time : 800
					});
				}
			});
		}
	}

	
	

	//提交表单
	function submitDirectories() {
		if ($('#user_name').val() === '' || $('#user_name').val() === null) {
			layer.alert('请输入用户名称！');
			return false;
		}
		if ($('#user_tel').val() === '' || $('#user_tel').val() === null) {
			layer.alert('请输入电话号码！');
			return false;
		}

		$.ajax({
			url : "${pageContext.request.contextPath}/insertDirectories",
			type : "post",
			dataType : "json",
			async : false,
			data : {
				"organizationId":$('#organizationId').val(),
				"name":$('#user_name').val(),
				"tel":$('#user_tel').val(),
				"id":dieId
			},
			success : function(data) {
				if (data.result === 0) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 2,
						time : 800
					});
				}
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
				return false;
			}
		});

		return false;
	}

	//删除
	function delDirectories(id) {
		layer.confirm("确定要删除该条信息吗？", function(index) {
			$.ajax({
				url : '${pageContext.request.contextPath}/deleteDirectories',
				type : "post",
				dataType : "json",
				async : false,
				data : {
					id : id
				},
				success : function(data) {
					if (data.result === 0) {
						layer.msg('删除成功!', {
							icon : 1,
							time : 800
						});
						$("#table").bootstrapTable('refresh');
					} else {
						layer.msg('系统繁忙，请稍后再试!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 2,
						time : 800
					});
					return false;
				}
			});
		});
	}
	
	function checkTel(){
		 var contactsTel = $("#user_tel").val(); 
		 var regex = /^1[3|4|5|8][0-9]\d{4,8}$/; 
		 if(contactsTel !=""){
			 if (!regex.test(contactsTel)) {
				 layer.msg('电话格式不正确!',{icon: 2,time:1000});
				 $("#user_tel").val("");
				 return false;
			 }
		 }
	}
</script>
</html>
